﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sector7.Framework.Core;
using Sector7.Framework.Enums;
using Sector7.Restaurant.DAO.OleDB;

namespace Sector7.Restaurant.DAO.Utility
{
    /// <summary>
    /// 主键生成工具
    /// </summary>
    public class PrimaryKeyUtil
    {
        private static ISeedDAO _seedDAO = new SeedDAO();

        /// <summary>
        /// 生成主键
        /// </summary>
        /// <param name="entityType">实体类型</param>
        /// <param name="args">附加参数</param>
        /// <returns></returns>
        public static object Generate(Type entityType, params object[] args)
        {
            Logger.WriteLine(LogSeverityType.DBG, "PrimaryKeyUtil.Generate(...)");

            string typeName = entityType.Name;

            object pk = null;
            int seed;

            switch (typeName)
            {
                case "RS_MenuItem":
                    // 0 - 元数据类型编号
                    // 1 - 元数据编号
                    // 2 - 单位编号
                    if (args.Length == 3)
                        pk = string.Format("MI{0:00}{1:0000}{2:00}", args[0], args[1], args[2]);
                    break;
                case "RS_SalesBill":
                    // 0 - 单据产生日期
                    // 1 - 单据类型
                    // 2 - 流水号
                    if (args == null || args.Length == 0)
                    {
                        seed = _seedDAO.Read(typeName);
                        pk = string.Format("{0:00000000}{1:00}{2:0000}",
                            DateTime.Now.ToString("yyyyMMdd"),
                            (int)BillType.SalesBill,
                            seed);
                    }
                    break;
                case "RS_PurchaseBill":
                    // 0 - 单据产生日期
                    // 1 - 单据类型
                    // 2 - 流水号
                    if (args == null || args.Length == 0)
                    {
                        seed = _seedDAO.Read(typeName);
                        pk = string.Format("{0:00000000}{1:00}{2:0000}",
                            DateTime.Now.ToString("yyyyMMdd"),
                            (int)BillType.PurchaseBill,
                            seed);
                    }
                    break;
                case "RS_Charge":
                    // 0 - 费用产生日期
                    // 1 - 费用类型
                    // 2 - 流水号
                    if (args.Length == 1)
                    {
                        seed = _seedDAO.Read(typeName);
                        pk = string.Format("{0:00000000}{1:00}{2:0000}",
                            DateTime.Now.ToString("yyyyMMdd"),
                            args[0],
                            seed);
                    }
                    break;
                default:
                    break;
            }

            Logger.WriteLine(LogSeverityType.DBG, "PK of {0} is [{1}]", typeName, pk);

            return pk;
        }
    }
}
